package com.hcmus.armobile;

import java.util.ArrayList;

import com.hcmus.armobile.R.style;
import common.Location;
import db.DBHandle;

import Server.HandleServer;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;
import global.*;
public class FavorProvince extends MainActivity{
	
	private ImageButton btnBackProvince;
	private LinearLayout wrapList;
	private ScrollView scroll;
	private Button btnSave;
	private ArrayList<Location> array_province = new ArrayList<Location>();
	private ArrayList<Integer> array_province_favor_selected = new ArrayList<Integer>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.checklist_favorite_province);
		wrapList = new LinearLayout(this);
		wrapList.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
		wrapList.setOrientation(LinearLayout.VERTICAL); 
		wrapList.setVisibility(View.VISIBLE);
		scroll = (ScrollView) findViewById(R.id.scrollListProvinceFavor);
		// data test
	
		array_province = DBHandle.GetListProvince(); //new ArrayList<Location>((ArrayList<Location>)extras.get("list_province"));
		
		//for(int i = 1 ; i <= 15 ; i++){
			//array_province.add(new Location(i,"Location "+String.valueOf(i)));
		//}
		String curProvince = global.curUser.getProvince();
		for(Location item : array_province ){
			//wrapList.addView(createItemCategories(item));
			LinearLayout layout = new LinearLayout(this);
			layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,48));
			layout.setOrientation(LinearLayout.HORIZONTAL);
			CheckBox cb = new CheckBox(this);
			cb.setTag(item);
			
			String p_id = ","+String.valueOf(item.getId())+",";
			if(curProvince.indexOf(p_id) >= 0){
				array_province_favor_selected.add(item.getId()); 
				cb.setChecked(true);
			}
			cb.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT));
			cb.setVisibility(View.VISIBLE);
			cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				
				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					// TODO Auto-generated method stub
					Location provinceTag = (Location) buttonView.getTag();
					if(isChecked){
						// checked
						//array_categories_favor_selected.add(Integer.valueOf(buttonView.getTag().toString()));
						array_province_favor_selected.add(Integer.valueOf(provinceTag.getId()));
					}else{
						// unchecked
						//array_categories_favor_selected.remove(Integer.valueOf(buttonView.getTag().toString()));
						array_province_favor_selected.remove(Integer.valueOf(provinceTag.getId())); 
					}
				}
			});
			TextView tv = new TextView(this);
			tv.setText(""+item.getName());
		    tv.setTextAppearance(this,style.text_label);
			tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT)); 
			tv.setGravity(Gravity.CENTER_VERTICAL);
			tv.setVisibility(View.VISIBLE);
			layout.addView(cb);
			layout.addView(tv);
			layout.setVisibility(View.VISIBLE);
			wrapList.addView(layout);
		}
		//wrapList.invalidate();
		scroll.addView(wrapList);
		// button back
		btnBackProvince = (ImageButton) findViewById(R.id.btnBackChecklistProvince);
		btnBackProvince.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent in = new Intent(v.getContext(), Favorite.class);
				startActivity(in);
			}
		});
		// btn Save
		btnSave = (Button) findViewById(R.id.button_save_province_favorite);
		btnSave.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (array_province_favor_selected.size() == 0) {
					Toast.makeText(v.getContext(),
							"------------------Empty--------------------", 10)
							.show();
					return;
				}
				String list_id_selected = "";
				int total = 0;
				for (int item : array_province_favor_selected) {
					list_id_selected += String.valueOf(item) + ",";
					total += item;
				}
				list_id_selected = list_id_selected.substring(0,list_id_selected.length()-1);
				Toast.makeText(v.getContext(), "List Id : " + list_id_selected,
						10).show();
				Toast.makeText(v.getContext(),
						"Total : " + String.valueOf(total), 10).show();
				/*CongTT*/
				
				boolean result = HandleServer.UpdateLocation(","+list_id_selected+",");
				if(result == true){
					Toast.makeText(v.getContext(),
							"Update Success !!! ", 10).show();
					
				}else{
					Toast.makeText(v.getContext(),
							"Update False !!! ", 10).show();
				}
				
				
				/**/
			}
		});
	}

}
